package com.headwind.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.headwind.entity.HwMood;
import com.headwind.mapper.master.HwMoodMasterMapper;
import com.headwind.mapper.read.HwMoodReadMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/14
 * @description : 说说说说实现类
 */
@Service
public class HwMoodService {
    private final HwMoodMasterMapper masterMapper;
    private final HwMoodReadMapper readMapper;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    public HwMoodService(HwMoodMasterMapper mapper, HwMoodReadMapper readMapper) {
        this.masterMapper = mapper;
        this.readMapper = readMapper;
    }

    /**
     * 查询所有说说
     *
     * @return 说说列表
     */
    public Dict findAllMoods() {
        PageHelper.startPage(0,1);
        List<HwMood> moodList = readMapper.findAllMood();
        PageInfo pageInfo = new PageInfo(moodList);
//        String result = gson.toJson(readMapper.findAllMood());
        return Dict.create().set("code", 200).set("data", pageInfo);
    }

    /**
     * 分页查询所有说说
     *
     * @return 说说列表
     */
    public Dict findAllMoodsByPage() {
        String result = gson.toJson(readMapper.findAllMoodByPage(new RowBounds(0,5)));
        return Dict.create().set("code", 200).set("data", result);
    }

    /**
     * 查询特定用户所有说说
     *
     * @param userId 用户名
     * @return 说说列表
     */
    public Dict findMoodsByUserId(String userId) {
        String result = gson.toJson(readMapper.findByUserId(userId));
        return Dict.create().set("code", 200).set("data", result);
    }

    /**
     * 发表说说
     *
     * @param mood 说说内容
     * @return 状态信息
     */
    public Dict saveMood(HwMood mood) {
        mood.setPublishTime(DateUtil.date());
        int result = masterMapper.saveMood(mood);
        if (result > 0) {
            return Dict.create().set("code", 201).set("message", "发表说说成功！");
        } else {
            return Dict.create().set("code", 500).set("message", "发表说说失败！");
        }
    }

    /**
     * 更新说说
     *
     * @param mood 说说内容
     * @return 状态信息
     */
    public Dict updateMood(HwMood mood) {
        mood.setPublishTime(DateUtil.date());
        int result = masterMapper.updateMood(mood);
        if (result > 0) {
            return Dict.create().set("code", 202).set("message", "更新说说成功！");
        } else {
            return Dict.create().set("code", 500).set("message", "更新说说失败！");
        }
    }

    /**
     * 删除特定说说
     *
     * @param id 说说Id
     * @return 状态信息
     */
    public Dict deleteMood(String id) {
        int result = masterMapper.deleteMood(id);
        if (result > 0) {
            return Dict.create().set("code", 204).set("message", "删除说说成功！");
        } else {
            return Dict.create().set("code", 500).set("message", "删除说说失败！");
        }
    }
}
